/* =============================================================================
 * BeeWorks MoneyPile - Domain Model
 *
 * 
 * =============================================================================
 *
 * Developers: Jurgen Lust
 *
 * 
 * (C) Copyright 2009, BeeWorks BVBA.
 *
 */

package be.beeworks.moneypile.model;

import be.beeworks.moneypile.model.enums.DocumentType;

import org.hibernate.search.annotations.Indexed;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;


/**
 * Details about an expense made by the company.
 *
 * @author jlust
 * @author $LastChangedBy: Jurgen.Lust $
 *
 * @version $LastChangedRevision: 15 $
 */
@Entity
@Table(name = "EXPENSES")
@Indexed
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Expense
        extends AbstractDocument {
    //~ Static fields/initializers ---------------------------------------------

    private static final long serialVersionUID = 5370933653287272022L;

    //~ Instance fields --------------------------------------------------------

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "contact_id_ref", nullable = false)
    @XmlIDREF
    private Contact contact;
    @Temporal(TemporalType.DATE)
    private Date paidDate;
    @NotNull
    @Min(0)
    private Float priceWithoutVat;
    @NotNull
    @Min(0)
    @Max(100)
    private Integer percentVat;

    //~ Constructors -----------------------------------------------------------

    public Expense() {
        super();
        this.percentVat = 21;
    }

    //~ Methods ----------------------------------------------------------------

    public Date getPaidDate() {
        return paidDate;
    }

    public void setPaidDate(Date paidDate) {
        this.paidDate = paidDate;
    }

    public Float getPriceWithoutVat() {
        return priceWithoutVat;
    }

    public void setPriceWithoutVat(final Float price) {
        this.priceWithoutVat = price;
    }

    public Integer getPercentVat() {
        return percentVat;
    }

    public void setPercentVat(final Integer percentVat) {
        this.percentVat = percentVat;
    }

    public Float getVatAmount() {
        if ((getPercentVat() == null) || (getPriceWithoutVat() == null)) {
            return 0f;
        }

        return (getPriceWithoutVat() * getPercentVat()) / 100;
    }

    public Float getPriceWithVat() {
        if (getPriceWithoutVat() == null) {
            return 0f;
        }

        return getPriceWithoutVat() + getVatAmount();
    }

    @Override
    public DocumentType getType() {
        return DocumentType.Expense;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }
}
